package com.moorror.codegenvel.view.window.codegen.view;


import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBTabbedPane;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenScene;
import com.moorror.codegenvel.tab.TabJsonWrap;

import javax.swing.*;
import java.awt.*;

/**
 * @Author jiyanghuang
 * @Date 2025/8/15 22:40
 */
public class CodeGenScenePanel extends JPanel {

    private Project project;

    private JTabbedPane tabbedPane;

    private TabJsonWrap templateJsonValue;

    private CodeGenScene codeGenScene;

    private TabWrapTabCodeGenSceneKeyFields keyFields;

    private TabWrapTabCodeGenSceneSQL tabSql;

    public CodeGenScenePanel(Project project, CodeGenScene codeGenScene) {
        this.project = project;
        this.codeGenScene = codeGenScene;
        initView();
    }

    private void initView(){
        setLayout(new BorderLayout(0,0));
        tabbedPane = new JTabbedPane(JBTabbedPane.TOP);
        keyFields = new TabWrapTabCodeGenSceneKeyFields(project, 0, "KeyFields", tabbedPane, new TabCodeGenSceneKeyFields(this.project, this.codeGenScene));
        templateJsonValue = new TabJsonWrap(project, 1, "VelocityContext", tabbedPane, codeGenScene.getJsonContext(), false);
        tabSql = new TabWrapTabCodeGenSceneSQL(project, 2, "SQL", tabbedPane, new TabCodeGenSceneSQL(this.project, this.codeGenScene, this.templateJsonValue));
        add(tabbedPane, BorderLayout.CENTER);
    }

    public void fetchValue(){
        codeGenScene.setJsonContext(templateJsonValue.text());
        keyFields.fetchValue();
        tabSql.fetchValue();
    }


    public void fillVelocityContext(){
        templateJsonValue.fillSource(codeGenScene.getJsonContext());
    }
}
